package com.my.system.dao.customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;

import com.my.system.dao.DataBaseConnection;
import com.my.system.object.client.User;

public class UserDao {
	private static Connection con = null;
	private static PreparedStatement psta = null;
	private static ResultSet rs = null;
	static {
		con = DataBaseConnection.getConnection();
	}

	public static List<User> searchUserByClientID(int clientId) {
		String sql = "select * from user where client_id=?";
		List<User> list = new ArrayList<User>();
		try {
			psta = con.prepareStatement(sql);
			psta.setInt(1, clientId);
			rs = psta.executeQuery();
			while (rs.next()) {
				int id = rs.getInt(1);
				String username = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				int client_id = rs.getInt(6);
				User user = new User(id, username, password, email, phone,
						client_id);
				list.add(user);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}

	public static User searchUserByUsername(String username1) {
		String sql = "select * from user where username=?";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, username1);
			rs = psta.executeQuery();
			if (rs.next()) {
				int id = rs.getInt(1);
				String username = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				int client_id = rs.getInt(6);
				User user = new User(id, username, password, email, phone,
						client_id);
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return null;
	}

	public static boolean isExsit(String username) {
		String sql = "select * from user where username=?";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, username);
			rs = psta.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return false;
	}

	public static boolean isClientExsit(int client_id) {
		String sql = "select * from user where client_id=?";
		try {
			psta = con.prepareStatement(sql);
			psta.setInt(1, client_id);
			rs = psta.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return false;
	}

	public static boolean Signup(User user) {
		String sql = "insert into user(" + "username," + "password," + "email,"
				+ "phone," + "client_id)" + "values(?,?,?,?,?)";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, user.getUsername());
			psta.setString(2, DigestUtils.md5Hex(user.getPassword()));
			psta.setString(3, user.getEmail());
			psta.setString(4, user.getPhone());
			psta.setInt(5, user.getClient_id());
			con.setAutoCommit(false);
			int i = psta.executeUpdate();
			con.commit();
			con.setAutoCommit(true);
			if (i > 0) {
				return true;
			}

		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static boolean updateUser(User user) {
		String sql = "update user set" + " username=?," + "password=?,"
				+ "email=?," + "phone=?," + "client_id=?" + " where id=?";
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, user.getUsername());
			psta.setString(2, user.getPassword());
			psta.setString(3, user.getEmail());
			psta.setString(4, user.getPhone());
			psta.setInt(5, user.getClient_id());
			psta.setInt(6, user.getId());
			con.setAutoCommit(false);
			int i = psta.executeUpdate();
			con.commit();
			con.setAutoCommit(true);
			if (i > 0) {
				return true;
			}

		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public static User searchUserByID(int id) {
		String sql = "select * from user where id=?";
		User user = null;
		try {
			psta = con.prepareStatement(sql);
			psta.setInt(1, id);
			rs = psta.executeQuery();
			if (rs.next()) {
				int id1 = rs.getInt(1);
				String username = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				int client_id = rs.getInt(6);
				user = new User(id1, username, password, email, phone,
						client_id);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return user;
	}

	public static User Login(String username1, String password1) {
		String sql = "select * from user where username=? and password=?";
		User user = null;
		try {
			psta = con.prepareStatement(sql);
			psta.setString(1, username1);
			psta.setString(2, DigestUtils.md5Hex(password1));
			rs = psta.executeQuery();
			if (rs.next()) {
				int id1 = rs.getInt(1);
				String username = rs.getString(2);
				String password = rs.getString(3);
				String email = rs.getString(4);
				String phone = rs.getString(5);
				int client_id = rs.getInt(6);
				user = new User(id1, username, password, email, phone,
						client_id);

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (psta != null) {
					psta.close();
				}
				if (rs != null) {
					psta.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return user;
	}
}
